<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
	<th:block th:include="include :: header('新增控制')" />
	<style>
		.imgStyle{
			max-width: 200px;
		}
	</style>
</head>
<body class="white-bg">
	<div class="wrapper wrapper-content animated fadeInRight ibox-content">
		<form class="form-horizontal m" id="form-kongZhi-add" enctype="multipart/form-data">
			<input id="treeId" name="parentId" type="hidden" th:value="${kongZhi.deptId}"   />
			<div class="form-group">
				<label class="col-sm-3 control-label">上级部门：</label>
				<div class="col-sm-8">
				    <div class="input-group">
						<input class="form-control" type="text" onclick="selectDeptTree()" id="treeName" readonly="true" th:value="${kongZhi.deptName}">
					    <span class="input-group-addon"><i class="fa fa-search"></i></span>
				    </div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">控制名称：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="deptName" id="deptName" required>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">显示排序：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="orderNum" required>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">命令（没有请不要填写）：</label><!--负责人-->
				<div class="col-sm-8">
					<textarea class="form-control" type="text" name="leader"></textarea>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">图标：</label>
				<div class="col-sm-8">
					<img id="tuBiaoImg" name="tuBiaoImg" th:src="@{/img/mo_ren1.png}" class="imgStyle" th:onerror="'this.src=\'' + @{'/img/mo_ren1.png'} + '\''"/>
					<input type="file" name="tuBiaoFile" id="tuBiaoFile" accept="image/*" />
				</div>
			</div>
			<!--
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">图标点击：</label>
				<div class="col-sm-8">
					<img id="tuBiaoDianJiImg" name="tuBiaoDianJiImg" th:src="@{/img/mo_ren2.png}" class="imgStyle" th:onerror="'this.src=\'' + @{'/img/mo_ren2.png'} + '\''"/>
					<input type="file" name="tuBiaoDianJiFile" id="tuBiaoDianJiFile" accept="image/*"/>
				</div>
			</div>
			-->
			<div class="form-group">
				<label class="col-sm-3 control-label">控制状态：</label>
				<div class="col-sm-8">
				    <div class="radio-box" th:each="dict : ${@dict.getType('sys_normal_disable')}">
						<input type="radio" th:id="${dict.dictCode}" name="status" th:value="${dict.dictValue}" th:checked="${dict.default}">
						<label th:for="${dict.dictCode}" th:text="${dict.dictLabel}"></label>
					</div>
				</div>
			</div>
		</form>
	</div>
	<th:block th:include="include :: footer" />
	<script type="text/javascript">
		var prefix = ctx + "qiChe/kongZhi";

		$("#tuBiaoFile").change(function (){
			tuBiaoChange(this,"tuBiaoImg")
		});
		$("#tuBiaoDianJiFile").change(function (){
			tuBiaoChange(this,"tuBiaoDianJiImg")
		});

		function tuBiaoChange (that,imgId) {
			var $file = $(that);
			var fileObj = $file[0];
			var windowURL = window.URL || window.webkitURL;
			var dataURL;
			var $img = $("#"+imgId);

			if (fileObj && fileObj.files && fileObj.files[0]) {
				dataURL = windowURL.createObjectURL(fileObj.files[0]);
				$img.attr('src', dataURL);
			} else {
				dataURL = $file.val();
				var imgObj = document.getElementById(imgId);
				// 两个坑:
				// 1、在设置filter属性时，元素必须已经存在在DOM树中，动态创建的Node，也需要在设置属性前加入到DOM中，先设置属性在加入，无效；
				// 2、src属性需要像下面的方式添加，上面的两种方式添加，无效；
				imgObj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
				imgObj.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = dataURL;
			}
		}
		
		$("#form-kongZhi-add").validate({
			onkeyup: false,
			rules:{
				deptName:{
					remote: {
		                url: prefix + "/checkDeptNameUnique",
		                type: "post",
		                dataType: "json",
		                data: {
		                	"parentId": function() {
		                		return $("input[name='parentId']").val();
		                    },
		                	"deptName" : function() {
		                        return $.common.trim($("#deptName").val());
		                    }
		                },
		                dataFilter: function(data, type) {
		                	return $.validate.unique(data);
		                }
		            }
				},
				orderNum:{
					digits:true
				},
				email:{
                    email:true,
        		},
        		phone:{
        			isPhone:true,
        		},
			},
			messages: {
		        "deptName": {
		            remote: "控制已经存在"
		        }
		    },
		    focusCleanup: true
		});
		
		function submitHandler() {
	        if ($.validate.form()) {
	        	//$.operate.save(prefix + "/add", $('#form-kongZhi-add').serialize());


				var formdata = new FormData(document.getElementById("form-kongZhi-add"));
				//var formdata = new FormData();
				//formdata.append("kongZhi", $('#form-kongZhi-add').serialize());
				//formdata.append("tuBiaoFile", $("#tuBiaoFile")[0].files[0]);

				//$.operate.save(prefix + "/add", formdata);

				$.ajax({
					url: prefix + "/add",
					data: formdata,
					type: "post",
					processData: false,
					contentType: false,
					success: function(result) {
						$.operate.successCallback(result);
					}
				})

	        }
	    }
	
		/*部门管理-新增-选择父部门树*/
		function selectDeptTree() {
			var options = {
				title: '部门选择',
				width: "380",
				url: prefix + "/selectDeptTree/" + $("#treeId").val(),
				callBack: doSubmit
			};
			$.modal.openOptions(options);
		}
		
		function doSubmit(index, layero){
			var body = layer.getChildFrame('body', index);
   			$("#treeId").val(body.find('#treeId').val());
   			$("#treeName").val(body.find('#treeName').val());
   			layer.close(index);
		}
	</script>
</body>
</html>
